package cn.spring.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.spring.bean.Commodity;
import cn.spring.bean.Employee;
import cn.spring.bean.Type;
import cn.spring.service.CommodityService;
import cn.spring.util.UploadUtil;

@Controller
@RequestMapping("/commodity")
public class CommodityController {
	@Autowired
	private CommodityService biz;
	
	@RequestMapping("/findAll")
	public String findAll(Model model,HttpServletRequest request){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		/*List<Map<String, Object>> piList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> xianList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> yinList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> list=biz.findAll(emp.getId());*/
		List<Commodity> piList=new ArrayList<Commodity>();
		List<Commodity> xianList=new ArrayList<Commodity>();
		List<Commodity> yinList=new ArrayList<Commodity>();
		List<Commodity> list=biz.getAll1();
		//int zong=0;
		for(Commodity co:list){
			int typeId=co.getTypeid();
			if(typeId==1){
				piList.add(co);
			}else if(typeId==2){
				xianList.add(co);
			}else{
				yinList.add(co);
			}
			//int num=Integer.parseInt(co.get("num").toString());
			//zong+=num;
		}
		model.addAttribute("piList", piList);
		model.addAttribute("xianList", xianList);
		model.addAttribute("yinList", yinList);
		//model.addAttribute("zong", zong);
		return "../index";
	}
	
	@RequestMapping("/sou")
	public String sou(Model model,HttpServletRequest request,@RequestParam("content")String content) throws UnsupportedEncodingException{
		//Employee emp=(Employee)request.getSession().getAttribute("emp");
		content = URLDecoder.decode(content , "UTF-8");
		List<Map<String, Object>> piList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> xianList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> yinList=new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> list=biz.findAllByContent(content);
		int zong=0;
		for(Map<String, Object> co:list){
			int typeId=Integer.parseInt(co.get("typeId").toString());
			if(typeId==1){
				piList.add(co);
			}else if(typeId==2){
				xianList.add(co);
			}else{
				yinList.add(co);
			}
			//int num=Integer.parseInt(co.get("num").toString());
			//zong+=num;
		}
		model.addAttribute("piList", piList);
		model.addAttribute("xianList", xianList);
		model.addAttribute("yinList", yinList);
		model.addAttribute("zong", 0);
		return "../index";
	}
	
	@RequestMapping("/tong")
	public String tong(Model model){
		List<Map<String, Object>> list=biz.tong();
		/*int zong=0;
		for(Map<String, Object> co:list){
			int num=Integer.parseInt(co.get("total").toString());
			zong+=num;
		}*/
		List<Type> tList=biz.getType();
		model.addAttribute("list", list);
		model.addAttribute("tList", tList);
		return "../admin/editors";
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Commodity getById(@RequestParam("id")int id){
		Commodity com=biz.findById(id);
		return com;
	}
	
	@RequestMapping("/saveAndUpdate")
	public String saveAndUpdate(HttpServletRequest request,@ModelAttribute Commodity com,Model model,@RequestParam(value = "file", required = false) MultipartFile file){
		if(com.getPrice()==null){
			com.setPrice((float) 0.0);
			if(file.getSize()>0){
				String image=UploadUtil.upload(request, file);
				com.setImage(image);
			}
			com.setIsMai(true);
			int count=0;
			if(com.getId()!=null){
				count=biz.update(com);
			}else{
				count=biz.insert(com);
			}
			if(count>0){
				List<Map<String, Object>> list=biz.tong();
				List<Type> tList=biz.getType();
				model.addAttribute("list", list);
				model.addAttribute("tList", tList);
				return "../admin/editors";
			}else{
				model.addAttribute("msg", "更新或添加失败，请重试");
				return "../admin/error";
			}
		}else{
			
			if(file.getSize()>0){
				String image=UploadUtil.upload(request, file);
				com.setImage(image);
			}
			com.setIsMai(true);
			int count=0;
			if(com.getId()!=null){
				count=biz.update(com);
			}else{
				count=biz.insert(com);
			}
			if(count>0){
				List<Map<String, Object>> list=biz.tong();
				List<Type> tList=biz.getType();
				model.addAttribute("list", list);
				model.addAttribute("tList", tList);
				return "../admin/editors";
			}else{
				model.addAttribute("msg", "更新或添加失败，请重试");
				return "../admin/error";
			}
		}
		
	}
	
	@RequestMapping("/shan")
	public String shan(@RequestParam("id")int id,Model model){
		int count=biz.updateIsMai(id);
		if(count>0){
			List<Map<String, Object>> list=biz.tongShan();
			List<Type> tList=biz.getType();
			model.addAttribute("list", list);
			model.addAttribute("tList", tList);
			return "../admin/editors";
		}else{
			model.addAttribute("msg", "删除失败，请重试");
			return "../admin/error";
		}
	}
	
	//更换上下架
	@RequestMapping("/updateState")
	public String updateState(@RequestParam(value="id",defaultValue="0")int id,@RequestParam("isMai")Boolean isMai,Model model){
		if(id==0){
			int count=biz.updateIsMaiAll(isMai);
			if(isMai){
				if(count==0){
					count=1;
				}
			}
			if(count>0){
				List<Map<String, Object>> list=biz.tong();
				List<Type> tList=biz.getType();
				model.addAttribute("list", list);
				model.addAttribute("tList", tList);
				return "../admin/editors";
			}else{
				model.addAttribute("msg", "操作失败，请重试");
				return "../admin/error";
			}
				
		}else{
			int count=biz.updateStatsById(isMai, id);
			if(count>0){
				List<Map<String, Object>> list=biz.tong();
				List<Type> tList=biz.getType();
				model.addAttribute("list", list);
				model.addAttribute("tList", tList);
				return "../admin/editors";
			}else{
				model.addAttribute("msg", "操作失败，请重试");
				return "../admin/error";
			}
		}
	}
}
